#define _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int count = 0;
    int ans = 0;
    int kthSmallest(TreeNode* root, int k) {
        count = k;
        dfs(root);
        return ans;
    }
    void dfs(TreeNode* root) {
        if (root == nullptr) return;
        dfs(root->left);
        count--;
        if (count == 0) {
            ans = root->val;
            return;
        }
        dfs(root->right);
    }
};